package platform;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoService;  
import org.apache.mina.core.service.IoServiceListener;  
import org.apache.mina.core.session.IdleStatus;  
import org.apache.mina.core.session.IoSession;  
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
  
public class LogicServerIoListener implements IoServiceListener{
	protected static final Logger log = LoggerFactory.getLogger(LogicServerIoListener.class);
	private NioSocketConnector conn;
	private ConnectListener connl;
	private String serverkey;
	public LogicServerIoListener(NioSocketConnector con, ConnectListener conl,String k)
	{
		conn = con;
		connl = conl;
		serverkey = k;
	}
    @Override  
    public void serviceActivated(IoService arg0) throws Exception {  
        // TODO Auto-generated method stub  
    }  
    @Override  
    public void serviceDeactivated(IoService arg0) throws Exception {  
        // TODO Auto-generated method stub  
    }  
    @Override  
    public void serviceIdle(IoService arg0, IdleStatus arg1) throws Exception {  
        // TODO Auto-generated method stub  
    }  
    @Override  
    public void sessionCreated(IoSession arg0) throws Exception {  
        // TODO Auto-generated method stub
    }  
  
    @Override  
    public void sessionDestroyed(IoSession arg0) throws Exception {
    	log.info("IoServiceListener sessionDestroyed "
    			+ conn.getDefaultRemoteAddress().getHostString() + ":" + conn.getDefaultRemoteAddress().getPort());
    	arg0.close(true);
    	//conn.dispose();
    	LogicServerConnect.logicServersSessions.put(serverkey, null);
    	ConnectFuture cf = conn.connect();
    	cf.addListener(connl);
    	log.info("IoServiceListener reConnecting "
    			+ conn.getDefaultRemoteAddress().getHostString() + ":" + conn.getDefaultRemoteAddress().getPort());
    }  
}
